﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;

namespace EndlessGalaxy
{
    public class Ship
    {
        public static Dictionary<string, Ship> ShipTypes;

        public string shipClass;
        public List<Cargo> cargo;
        public double speed;
        public int crew;
        public int mineLevel;
        public bool canexplore;
        public bool canfight;
        public double cargosize;
        public Point location;
        public int orbitedindex;
        public Point destination;
        public int destindex;
        public int index; // temporary storage of the index for the selection routine
        public activity action;

        public static void PopulateTemplate(string name, double spd, int crew, int mining, bool explore, bool fight, double cargo)
        {
            Ship that = new Ship();
            that.speed = spd;
            that.crew = crew;
            that.mineLevel = mining;
            that.canexplore = explore;
            that.canfight = fight;
            that.cargosize = cargo;
            ShipTypes.Add(name, that);
        }

        public Ship()
        {
            cargo = new List<Cargo>();
            action = new activity();
        }

        public Ship(string shipclass, Point Location)
        {
            cargo = new List<Cargo>();
            location = Location;
            destination = location;
            shipClass = shipclass;
            speed = ShipTypes[shipclass].speed;
            crew = ShipTypes[shipclass].crew;
            mineLevel = ShipTypes[shipclass].mineLevel;
            canexplore = ShipTypes[shipclass].canexplore;
            canfight = ShipTypes[shipclass].canfight;
            cargosize = ShipTypes[shipclass].cargosize;
            action = new activity();
        }

        public Ship(string shipclass, Planet homeworld)
        {
            cargo = new List<Cargo>();
            location = homeworld.location;
            destination = homeworld.location;
            shipClass = shipclass;
            speed = ShipTypes[shipclass].speed;
            crew = ShipTypes[shipclass].crew;
            mineLevel = ShipTypes[shipclass].mineLevel;
            canexplore = ShipTypes[shipclass].canexplore;
            canfight = ShipTypes[shipclass].canfight;
            cargosize = ShipTypes[shipclass].cargosize;
            action = new activity();
        }

        public override string ToString()
        {
            double freespace = cargosize;
            foreach (Cargo thing in cargo)
            {
                freespace -= thing.amount;
            }
            return shipClass + " with " + freespace + " tonnes free cargo";
        }
    }

    public class activity
    {
        public string action;
        public Cargo cargo;
        public Planet ptarget;
        public Ship starget;

        public activity()
        {
            action = "idle";
        }

        public activity(string act)
        {
            action = act;
        }

        public activity(string act, Planet place)
        {
            action = act;
            ptarget = place;
        }

        public activity(string act, Cargo stuff, Planet place)
        {
            action = act;
            ptarget = place;
            cargo = stuff;
        }

        public override string ToString()
        {
            if (cargo != null)
            {
                if (ptarget != null)
                {
                    return action + " " + cargo.name + " with " + ptarget;
                }
                if (starget != null)
                {
                    return action + " " + cargo.name + " with " + starget;
                }
                return action + " " + cargo.name;
            }
            if (ptarget != null)
            {
                return action + " with " + ptarget;
            }
            if (starget != null)
            {
                return action + " with " + starget;
            }
            return action;
        }
    }
}
